{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8951ec11-fcc2-4a0f-99c5-fa245e6350ce",
   "metadata": {},
   "source": [
    "### 演示KNN算法原理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "14d79746-a99b-479d-ad86-2b3d0fa94588",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np;\n",
    "import matplotlib.pyplot as plt;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5dcbac12-0075-43db-9a3a-d4baf4f18936",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 样本特性\n",
    "data_X=[\n",
    "    [1.3,6],\n",
    "    [3.5,5],\n",
    "    [4.2,2],\n",
    "    [5,3.3],\n",
    "    [2,9],\n",
    "    [5,7.5],\n",
    "    [7.2,4],\n",
    "    [8.1,8],\n",
    "    [9,2.5]\n",
    "]\n",
    "\n",
    "data_y=[0,0,0,0,1,1,1,1,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "226e23dc-45d1-4c04-91ce-2eca36eb6d8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 训练集\n",
    "X_train = np.array(data_X)\n",
    "y_train = np.array(data_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "df0b4df9-d063-4f6a-9ccf-e1e0b23598de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.3, 6. ],\n",
       "       [3.5, 5. ],\n",
       "       [4.2, 2. ],\n",
       "       [5. , 3.3]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train[y_train == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0af5c513-af49-44c4-982f-5f1939269670",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.3, 3.5, 4.2, 5. ])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train[y_train == 0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "496a953d-ea94-4598-bb57-f62ece44edd9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6. , 5. , 2. , 3.3])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train[y_train == 0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "41ee6732-0bdf-451b-8a29-bd8168dbae8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAhYAAAGdCAYAAABO2DpVAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjAsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvlHJYcgAAAAlwSFlzAAAPYQAAD2EBqD+naQAAIj1JREFUeJzt3X90U/X9x/FXCFIqNlGY0ZYESpkDhaGeA0dbqLQDGYoctBYVlCHodjyrWuS4IeqmTrC6Hx7Y2RkO9DAoB/GsC/jrAANnu+KmFhWHP8YPiyMpVbYdSApu8RDu9498WxvbQlM+yW2b5+OcHJebG/KObua5ez+5cViWZQkAAMCAPnYPAAAAeg/CAgAAGENYAAAAYwgLAABgDGEBAACMISwAAIAxhAUAADCGsAAAAMb0TfULnjx5UocOHVJWVpYcDkeqXx4AAHSBZVlqampSTk6O+vTp+LhEysPi0KFD8vl8qX5ZAABgQCAQkNfr7fDxlIdFVlaWpNhgLpcr1S8PAAC6IBwOy+fztXyOdyTlYdF8+sPlchEWAAD0MKdbxsDiTQAAYAxhAQAAjCEsAACAMYQFAAAwhrAAAADGEBYAAMAYwgIAABhDWAAAAGNSfoGsdBWNRlVbW6vGxkZlZ2ersLBQTqfT7rEAADAq4SMWTU1NWrBggYYOHarMzEwVFBSorq4uGbP1Gn6/X7m5uSouLtbs2bNVXFys3Nxc+f1+u0cDAMCohMPizjvv1L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Cvu7YsWPWBRdcYP3qV7+yexTjzjrrLCs/Pz9u2z333GNdeeWVNk2UGlOmTLGuu+46u8dIiueff97yer3W888/b/3973+31q5daw0cOND6/e9/b/doxu3fv9+66qqrLEmW0+m0xo0bZ916663WxRdfnPJZCIsUuvvuuy2v12vV19fbPUrKTJo0yfrBD35g9xjGbdy4seV/wM03SZbD4bCcTqd14sQJu0dMmsmTJ1t33XWX3WMYN2TIEOuOO+6I2/bb3/7WysnJsWmi5Pv000+tPn36WJs2bbJ7lKTwer3Wb37zm7htjz/+uDVixAibJkq+Y8eOWYcOHbIsy7Juuukm69prr035DJwKSQHLsnTPPfdo48aNqq6u1rBhw+weKWUsy1IkErF7DOMmTZqk3bt3x22bN2+eRo4cqUWLFvXab05EIhF9/PHHKiwstHsU48aPH9/ma+B79+7V0KFDbZoo+VavXi2Px6Np06bZPUpSfPHFF+rTJ34podPp7JVfN202YMAADRgwQEeOHNHWrVv185//POUz2BoWx44d0/79+1vuHzhwQLt27dLAgQM1ZMgQGyczq6ysTOvXr9eLL76orKwsffbZZ5Ikt9utzMxMm6cz58EHH9Q111wjn8+npqYmbdiwQdXV1dqyZYvdoxmXlZXVZo3MgAEDNGjQoF61dub+++/X9OnTNWTIEB0+fFhLlixROBzW3Llz7R7NuPvuu08FBQV64okndNNNN+ntt9/WypUrtXLlSrtHS4qTJ09q9erVmjt3rvr27Z3/H3P69OlaunSphgwZolGjRum9997T008/rfnz59s9mnFbt26VZVkaMWKE9u/frx/96EcaMWKE5s2bl/phUn6MpJXXX3/dktTmNnfuXDvHMq699yjJWr16td2jGTV//nxr6NChVr9+/azzzz/fmjRpkvWnP/3J7rFSpjeusbj55put7Oxs66yzzrJycnKskpKSXrlmptnLL79sjR492srIyLBGjhxprVy50u6Rkmbr1q2WJGvPnj12j5I04XDYKi8vt4YMGWL179/fysvLsx566CErEonYPZpxL7zwgpWXl2f169fPuvDCC62ysjLr6NGjtszCz6YDAABjuI4FAAAwhrAAAADGEBYAAMAYwgIAABhDWAAAAGMICwAAYAxhAQAAjCEsAACAMYQFAAAwhrAAAADGEBYAAMAYwgIAABjzf/Qn1UAFgIgnAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train == 0 , 0],X_train[y_train == 0,1],color=\"red\",marker=\"x\")\n",
    "plt.scatter(X_train[y_train == 1 , 0],X_train[y_train == 1,1],color=\"black\",marker=\"o\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fa040ebd-6677-40b1-b41f-ff97792c025d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 新样本点\n",
    "data_new = np.array([4,5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c9be50bc-68fb-4950-b53a-fb50dd50638b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAhYAAAGdCAYAAABO2DpVAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjAsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvlHJYcgAAAAlwSFlzAAAPYQAAD2EBqD+naQAAIs5JREFUeJzt3X90U/X9x/FXCFIqNlEYkZYESpkDhaGeA19toUIHMhQ5aC0qqEPQ7XhWtchxQ9TNX2B1PzywszMc6GHQfgG/6wL+OsDQCSs6tag4/DF+WBxJqbLtQFJwi6fp/f6RtTa0haZ8ktsfz8c5OZqbG/KO28xz935y47AsyxIAAIABveweAAAAdB+EBQAAMIawAAAAxhAWAADAGMICAAAYQ1gAAABjCAsAAGAMYQEAAIzpneoXbGho0OHDh5WRkSGHw5HqlwcAAB1gWZbq6uqUlZWlXr3aPi6R8rA4fPiwfD5fql8WAAAYEAgE5PV623w85WGRkZEhKTaYy+VK9csDAIAOCIfD8vl8TZ/jbUl5WDSe/nC5XIQFAABdzOmWMbB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NDo0aPtHse4PXv2KDc3V//5z390zjnnaOPGjbrooovsHispNmzYoPfee8+285ipcNlll2nt2rX61re+pS+++EJLlixRXl6ePvroIw0YMMDu8Yyqrq7WihUrtHDhQj3wwAN65513dM899ygtLU3f+9737B4vaTZt2qRjx47ptttus3sU4xYtWqRQKKSRI0fK6XQqGo1q6dKlmj17tt2jGZWRkaHc3Fw9/vjjuvDCC3X++edr/fr1evvtt3XBBRekfqCUHyNpg3rIqZAf/vCH1tChQ61AIGD3KEkRiUSs/fv3W1VVVdb9999vfeMb37A++ugju8cy7tChQ5bH47F2797dtK07ngo52fHjx63zzz/f+uUvf2n3KMadddZZVm5ubty2u+++27r88sttmig1pk6dal1zzTV2j5EU69evt7xer7V+/Xrrr3/9q7V27Vqrf//+1u9+9zu7RzPuwIED1hVXXGFJspxOpzVu3Djr5ptvti688MKUz0JYpNBdd91leb1eq7q62u5RUmby5MnWD37wA7vHMG7jxo1N/wNuvEmyHA6H5XQ6rfr6ertHTJopU6ZYd955p91jGDdkyBDr9ttvj9v2m9/8xsrKyrJpouT77LPPrF69elmbNm2ye5Sk8Hq91q9//eu4bY8//rg1YsQImyZKvuPHj1uHDx+2LMuybrjhBuvqq69O+QycCkkBy7J09913a+PGjdq+fbuGDRtm90gpY1mWIpGI3WMYN3nyZO3Zsydu27x58zRy5EgtWrSo235zIhKJ6JNPPlF+fr7doxg3fvz4Fl8D37dvn4YOHWrTRMm3evVqeTweTZ8+3e5RkuLLL79Ur17xSwmdTme3/Lppo379+qlfv346evSotm7dqp/97Gcpn8HWsDh+/LgOHDjQdP/gwYPavXu3+vfvryFDhtg4mVnFxcVat26dXnjhBWVkZOjzzz+XJLndbqWnp9s8nTkPPPCArrrqKvl8PtXV1WnDhg3avn27tmzZYvdoxmVkZLRYI9OvXz8NGDCgW62due+++zRjxgwNGTJER44c0ZIlSxQOhzV37ly7RzPu3nvvVV5enp544gndcMMNeuedd7Ry5UqtXLnS7tGSoqGhQatXr9bcuXPVu3f3/P+YM2bM0NKlSzVkyBCNGjVK77//vp5++mnNnz/f7tGM27p1qyzL0ogRI3TgwAH96Ec/0ogRIzRv3rzUD5PyYyTNvP7665akFre5c+faOZZxrb1HSdbq1avtHs2o+fPnW0OHDrX69OljDRw40Jo8ebL1xz/+0e6xUqY7rrG48cYbrczMTOuss86ysrKyrMLCwm65ZqbRSy+9ZI0ePdpKS0uzRo4caa1cudLukZJm69atliRr7969do+SNOFw2CopKbGGDBli9e3b18rJybEefPBBKxKJ2D2acc8//7yVk5Nj9enTxxo0aJBVXFxsHTt2zJZZ+Nl0AABgDNexAAAAxhAWAADAGMICAAAYQ1gAAABjCAsAAGAMYQEAAIwhLAAAgDGEBQAAMIawAAAAxhAWAADAGMICAAAYQ1gAAABj/h9T0ytfByo6uwAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train == 0 , 0],X_train[y_train == 0,1],color=\"red\",marker=\"x\")\n",
    "plt.scatter(X_train[y_train == 1 , 0],X_train[y_train == 1,1],color=\"black\",marker=\"o\")\n",
    "plt.scatter(data_new[0],data_new[1],color=\"blue\",marker=\"^\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60a4c11c-2a9c-451d-a4d6-253536275d98",
   "metadata": {},
   "source": [
    "### 1.计算新样本点与已知样本点的距离"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "41a7c022-0a70-4f59-949c-fa37770b962f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.879236009777594\n",
      "0.5\n",
      "3.006659275674582\n",
      "1.9723082923316022\n",
      "4.47213595499958\n",
      "2.692582403567252\n",
      "3.3526109228480423\n",
      "5.080354318352215\n",
      "5.5901699437494745\n"
     ]
    }
   ],
   "source": [
    "for data in X_train:\n",
    "    print(np.sqrt(np.sum((data - data_new)**2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "04164d7a-4dd8-4d02-90f3-6954454e31ea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7.29 1.  ]\n",
      "[0.25 0.  ]\n",
      "[0.04 9.  ]\n",
      "[1.   2.89]\n",
      "[ 4. 16.]\n",
      "[1.   6.25]\n",
      "[10.24  1.  ]\n",
      "[16.81  9.  ]\n",
      "[25.    6.25]\n"
     ]
    }
   ],
   "source": [
    "for data in X_train:\n",
    "    print((data - data_new)**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0f7d9751-7b3b-4d59-b132-0f083fbe59c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2.7  1. ]\n",
      "[-0.5  0. ]\n",
      "[ 0.2 -3. ]\n",
      "[ 1.  -1.7]\n",
      "[-2.  4.]\n",
      "[1.  2.5]\n",
      "[ 3.2 -1. ]\n",
      "[4.1 3. ]\n",
      "[ 5.  -2.5]\n"
     ]
    }
   ],
   "source": [
    "for data in X_train:\n",
    "    print((data - data_new))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2623a84d-da69-4c28-8b10-d138ab077673",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[np.float64(2.879236009777594),\n",
       " np.float64(0.5),\n",
       " np.float64(3.006659275674582),\n",
       " np.float64(1.9723082923316022),\n",
       " np.float64(4.47213595499958),\n",
       " np.float64(2.692582403567252),\n",
       " np.float64(3.3526109228480423),\n",
       " np.float64(5.080354318352215),\n",
       " np.float64(5.5901699437494745)]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "distances= [np.sqrt(np.sum((data-data_new)**2)) for data in X_train]\n",
    "distances"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35310081-93ed-4b6d-8d2a-d0d8a4f30a71",
   "metadata": {},
   "source": [
    "### 2.按距离排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "36bf9998-09a3-4e35-9977-16d43e16cd4e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.5       , 1.97230829, 2.6925824 , 2.87923601, 3.00665928,\n",
       "       3.35261092, 4.47213595, 5.08035432, 5.59016994])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(distances)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b5001554-e77b-4a04-8460-ea599449f86e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 0, 2, 6, 4, 7, 8])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sort_index = np.argsort(distances)\n",
    "sort_index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31df4879-120b-44af-8307-c76faabd6bb4",
   "metadata": {},
   "source": [
    "### 3.确定Ｋ值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "97dd319d-8f06-4599-af5d-5656531dfeb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "k = 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "270ee3b0-43f8-4f17-92ba-6304c219e52e",
   "metadata": {},
   "source": [
    "### 4.距离最近的k个点投票"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "226e40ab-56aa-4548-82ce-8804a3c91c86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[np.int64(0), np.int64(0), np.int64(1), np.int64(0), np.int64(0)]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first_k = [y_train[i] for i in sort_index[:k]]\n",
    "first_k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "29a0d296-b3ca-4cc4-9f97-443279ec44ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({np.int64(0): 4, np.int64(1): 1})"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "Counter(first_k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "90883abc-1ca2-4cc3-81e6-c9d9fe87f3c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(np.int64(0), 4)]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Counter(first_k).most_common(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "730a0a60-a5ee-45fc-a3fc-f29cb50f97d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(0)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_y = Counter(first_k).most_common(1)[0][0]\n",
    "predict_y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e2aeda8-f67f-4cf6-8aa7-006c0ae17969",
   "metadata": {},
   "source": [
    "## scikit-learn中的KNN算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "794260e1-b942-47f5-a8c6-055ea245f67c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-1 {\n",
       "  /* Definition of color scheme common for light and dark mode */\n",
       "  --sklearn-color-text: #000;\n",
       "  --sklearn-color-text-muted: #666;\n",
       "  --sklearn-color-line: gray;\n",
       "  /* Definition of color scheme for unfitted estimators */\n",
       "  --sklearn-color-unfitted-level-0: #fff5e6;\n",
       "  --sklearn-color-unfitted-level-1: #f6e4d2;\n",
       "  --sklearn-color-unfitted-level-2: #ffe0b3;\n",
       "  --sklearn-color-unfitted-level-3: chocolate;\n",
       "  /* Definition of color scheme for fitted estimators */\n",
       "  --sklearn-color-fitted-level-0: #f0f8ff;\n",
       "  --sklearn-color-fitted-level-1: #d4ebff;\n",
       "  --sklearn-color-fitted-level-2: #b3dbfd;\n",
       "  --sklearn-color-fitted-level-3: cornflowerblue;\n",
       "\n",
       "  /* Specific color for light theme */\n",
       "  --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, white)));\n",
       "  --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, black)));\n",
       "  --sklearn-color-icon: #696969;\n",
       "\n",
       "  @media (prefers-color-scheme: dark) {\n",
       "    /* Redefinition of color scheme for dark theme */\n",
       "    --sklearn-color-text-on-default-background: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-background: var(--sg-background-color, var(--theme-background, var(--jp-layout-color0, #111)));\n",
       "    --sklearn-color-border-box: var(--sg-text-color, var(--theme-code-foreground, var(--jp-content-font-color1, white)));\n",
       "    --sklearn-color-icon: #878787;\n",
       "  }\n",
       "}\n",
       "\n",
       "#sk-container-id-1 {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 pre {\n",
       "  padding: 0;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-hidden--visually {\n",
       "  border: 0;\n",
       "  clip: rect(1px 1px 1px 1px);\n",
       "  clip: rect(1px, 1px, 1px, 1px);\n",
       "  height: 1px;\n",
       "  margin: -1px;\n",
       "  overflow: hidden;\n",
       "  padding: 0;\n",
       "  position: absolute;\n",
       "  width: 1px;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-dashed-wrapped {\n",
       "  border: 1px dashed var(--sklearn-color-line);\n",
       "  margin: 0 0.4em 0.5em 0.4em;\n",
       "  box-sizing: border-box;\n",
       "  padding-bottom: 0.4em;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-container {\n",
       "  /* jupyter's `normalize.less` sets `[hidden] { display: none; }`\n",
       "     but bootstrap.min.css set `[hidden] { display: none !important; }`\n",
       "     so we also need the `!important` here to be able to override the\n",
       "     default hidden behavior on the sphinx rendered scikit-learn.org.\n",
       "     See: https://github.com/scikit-learn/scikit-learn/issues/21755 */\n",
       "  display: inline-block !important;\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-text-repr-fallback {\n",
       "  display: none;\n",
       "}\n",
       "\n",
       "div.sk-parallel-item,\n",
       "div.sk-serial,\n",
       "div.sk-item {\n",
       "  /* draw centered vertical line to link estimators */\n",
       "  background-image: linear-gradient(var(--sklearn-color-text-on-default-background), var(--sklearn-color-text-on-default-background));\n",
       "  background-size: 2px 100%;\n",
       "  background-repeat: no-repeat;\n",
       "  background-position: center center;\n",
       "}\n",
       "\n",
       "/* Parallel-specific style estimator block */\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item::after {\n",
       "  content: \"\";\n",
       "  width: 100%;\n",
       "  border-bottom: 2px solid var(--sklearn-color-text-on-default-background);\n",
       "  flex-grow: 1;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel {\n",
       "  display: flex;\n",
       "  align-items: stretch;\n",
       "  justify-content: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  position: relative;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:first-child::after {\n",
       "  align-self: flex-end;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:last-child::after {\n",
       "  align-self: flex-start;\n",
       "  width: 50%;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-parallel-item:only-child::after {\n",
       "  width: 0;\n",
       "}\n",
       "\n",
       "/* Serial-specific style estimator block */\n",
       "\n",
       "#sk-container-id-1 div.sk-serial {\n",
       "  display: flex;\n",
       "  flex-direction: column;\n",
       "  align-items: center;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  padding-right: 1em;\n",
       "  padding-left: 1em;\n",
       "}\n",
       "\n",
       "\n",
       "/* Toggleable style: style used for estimator/Pipeline/ColumnTransformer box that is\n",
       "clickable and can be expanded/collapsed.\n",
       "- Pipeline and ColumnTransformer use this feature and define the default style\n",
       "- Estimators will overwrite some part of the style using the `sk-estimator` class\n",
       "*/\n",
       "\n",
       "/* Pipeline and ColumnTransformer style (default) */\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable {\n",
       "  /* Default theme specific background. It is overwritten whether we have a\n",
       "  specific estimator or a Pipeline/ColumnTransformer */\n",
       "  background-color: var(--sklearn-color-background);\n",
       "}\n",
       "\n",
       "/* Toggleable label */\n",
       "#sk-container-id-1 label.sk-toggleable__label {\n",
       "  cursor: pointer;\n",
       "  display: flex;\n",
       "  width: 100%;\n",
       "  margin-bottom: 0;\n",
       "  padding: 0.5em;\n",
       "  box-sizing: border-box;\n",
       "  text-align: center;\n",
       "  align-items: start;\n",
       "  justify-content: space-between;\n",
       "  gap: 0.5em;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label .caption {\n",
       "  font-size: 0.6rem;\n",
       "  font-weight: lighter;\n",
       "  color: var(--sklearn-color-text-muted);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label-arrow:before {\n",
       "  /* Arrow on the left of the label */\n",
       "  content: \"▸\";\n",
       "  float: left;\n",
       "  margin-right: 0.25em;\n",
       "  color: var(--sklearn-color-icon);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {\n",
       "  color: var(--sklearn-color-text);\n",
       "}\n",
       "\n",
       "/* Toggleable content - dropdown */\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content {\n",
       "  max-height: 0;\n",
       "  max-width: 0;\n",
       "  overflow: hidden;\n",
       "  text-align: left;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content pre {\n",
       "  margin: 0.2em;\n",
       "  border-radius: 0.25em;\n",
       "  color: var(--sklearn-color-text);\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-toggleable__content.fitted pre {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {\n",
       "  /* Expand drop-down */\n",
       "  max-height: 200px;\n",
       "  max-width: 100%;\n",
       "  overflow: auto;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {\n",
       "  content: \"▾\";\n",
       "}\n",
       "\n",
       "/* Pipeline/ColumnTransformer-specific style */\n",
       "\n",
       "#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator-specific style */\n",
       "\n",
       "/* Colorize estimator box */\n",
       "#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted input.sk-toggleable__control:checked~label.sk-toggleable__label {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label label.sk-toggleable__label,\n",
       "#sk-container-id-1 div.sk-label label {\n",
       "  /* The background is the default theme color */\n",
       "  color: var(--sklearn-color-text-on-default-background);\n",
       "}\n",
       "\n",
       "/* On hover, darken the color of the background */\n",
       "#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "/* Label box, darken color on hover, fitted */\n",
       "#sk-container-id-1 div.sk-label.fitted:hover label.sk-toggleable__label.fitted {\n",
       "  color: var(--sklearn-color-text);\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Estimator label */\n",
       "\n",
       "#sk-container-id-1 div.sk-label label {\n",
       "  font-family: monospace;\n",
       "  font-weight: bold;\n",
       "  display: inline-block;\n",
       "  line-height: 1.2em;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-label-container {\n",
       "  text-align: center;\n",
       "}\n",
       "\n",
       "/* Estimator-specific */\n",
       "#sk-container-id-1 div.sk-estimator {\n",
       "  font-family: monospace;\n",
       "  border: 1px dotted var(--sklearn-color-border-box);\n",
       "  border-radius: 0.25em;\n",
       "  box-sizing: border-box;\n",
       "  margin-bottom: 0.5em;\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-0);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-0);\n",
       "}\n",
       "\n",
       "/* on hover */\n",
       "#sk-container-id-1 div.sk-estimator:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-2);\n",
       "}\n",
       "\n",
       "#sk-container-id-1 div.sk-estimator.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-2);\n",
       "}\n",
       "\n",
       "/* Specification for estimator info (e.g. \"i\" and \"?\") */\n",
       "\n",
       "/* Common style for \"i\" and \"?\" */\n",
       "\n",
       ".sk-estimator-doc-link,\n",
       "a:link.sk-estimator-doc-link,\n",
       "a:visited.sk-estimator-doc-link {\n",
       "  float: right;\n",
       "  font-size: smaller;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1em;\n",
       "  height: 1em;\n",
       "  width: 1em;\n",
       "  text-decoration: none !important;\n",
       "  margin-left: 0.5em;\n",
       "  text-align: center;\n",
       "  /* unfitted */\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted,\n",
       "a:link.sk-estimator-doc-link.fitted,\n",
       "a:visited.sk-estimator-doc-link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "div.sk-estimator:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link:hover,\n",
       ".sk-estimator-doc-link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "div.sk-estimator.fitted:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover,\n",
       "div.sk-label-container:hover .sk-estimator-doc-link.fitted:hover,\n",
       ".sk-estimator-doc-link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "/* Span, style for the box shown on hovering the info icon */\n",
       ".sk-estimator-doc-link span {\n",
       "  display: none;\n",
       "  z-index: 9999;\n",
       "  position: relative;\n",
       "  font-weight: normal;\n",
       "  right: .2ex;\n",
       "  padding: .5ex;\n",
       "  margin: .5ex;\n",
       "  width: min-content;\n",
       "  min-width: 20ex;\n",
       "  max-width: 50ex;\n",
       "  color: var(--sklearn-color-text);\n",
       "  box-shadow: 2pt 2pt 4pt #999;\n",
       "  /* unfitted */\n",
       "  background: var(--sklearn-color-unfitted-level-0);\n",
       "  border: .5pt solid var(--sklearn-color-unfitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link.fitted span {\n",
       "  /* fitted */\n",
       "  background: var(--sklearn-color-fitted-level-0);\n",
       "  border: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "\n",
       ".sk-estimator-doc-link:hover span {\n",
       "  display: block;\n",
       "}\n",
       "\n",
       "/* \"?\"-specific style due to the `<a>` HTML tag */\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link {\n",
       "  float: right;\n",
       "  font-size: 1rem;\n",
       "  line-height: 1em;\n",
       "  font-family: monospace;\n",
       "  background-color: var(--sklearn-color-background);\n",
       "  border-radius: 1rem;\n",
       "  height: 1rem;\n",
       "  width: 1rem;\n",
       "  text-decoration: none;\n",
       "  /* unfitted */\n",
       "  color: var(--sklearn-color-unfitted-level-1);\n",
       "  border: var(--sklearn-color-unfitted-level-1) 1pt solid;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link.fitted {\n",
       "  /* fitted */\n",
       "  border: var(--sklearn-color-fitted-level-1) 1pt solid;\n",
       "  color: var(--sklearn-color-fitted-level-1);\n",
       "}\n",
       "\n",
       "/* On hover */\n",
       "#sk-container-id-1 a.estimator_doc_link:hover {\n",
       "  /* unfitted */\n",
       "  background-color: var(--sklearn-color-unfitted-level-3);\n",
       "  color: var(--sklearn-color-background);\n",
       "  text-decoration: none;\n",
       "}\n",
       "\n",
       "#sk-container-id-1 a.estimator_doc_link.fitted:hover {\n",
       "  /* fitted */\n",
       "  background-color: var(--sklearn-color-fitted-level-3);\n",
       "}\n",
       "</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>KNeighborsClassifier()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator fitted sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label fitted sk-toggleable__label-arrow\"><div><div>KNeighborsClassifier</div></div><div><a class=\"sk-estimator-doc-link fitted\" rel=\"noreferrer\" target=\"_blank\" href=\"https://scikit-learn.org/1.6/modules/generated/sklearn.neighbors.KNeighborsClassifier.html\">?<span>Documentation for KNeighborsClassifier</span></a><span class=\"sk-estimator-doc-link fitted\">i<span>Fitted</span></span></div></label><div class=\"sk-toggleable__content fitted\"><pre>KNeighborsClassifier()</pre></div> </div></div></div></div>"
      ],
      "text/plain": [
       "KNeighborsClassifier()"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn_classifier = KNeighborsClassifier(n_neighbors=5)\n",
    "# 训练模型，第一个参数是训练样本数据集，第二个是样本数据标签集\n",
    "knn_classifier.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "06d7d491-a321-4fae-8c89-b8cf7e251654",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 预测\n",
    "knn_classifier.predict(data_new.reshape(1,-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "444da33c-0dcb-4f82-8004-1fa8a0e8e2aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new.reshape(1,-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebd93143-3dda-446c-ad63-c8ef2ecad20c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
